package com.itic.appbase.applications.sys.relation.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.applications.sys.relation.service.RoleResourceService;
import com.itic.appbase.applications.sys.resource.vo.ResourceVo;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.controller.BaseController;
import com.itic.appbase.framework.utils.StringHelper;

/**
 * 角色资源表
 * <p>Company: itic</p>
 * 
 * @author: shijun
 * @date: 2015年5月27日 上午8:45:11
 * @version: V1.0
 */
@Controller
@RequestMapping(value = "/sys/roleResource")
public class RoleResourceController
        extends BaseController {
    @Autowired
    private RoleResourceService roleResourceService;

    /**
     * 根据角色id,查询角色在整个资源表中所拥有的资源
     * 
     * @param roleIds
     * @return
     */
    @RequestMapping(value = "/getResourceByRole")
    @ResponseBody
    public List<ResourceVo> selectResourceByRoleId(String roleIds) {
        return roleResourceService.selectResourceByRoleId(Arrays.asList(roleIds.split(",")));
    }

    /**
     * 角色授权
     * 
     * @param roleId 角色id
     * @param resourceIds 资源id
     * @return
     */
    @RequestMapping(value = "/authorize")
    @ResponseBody
    public Map<String, Object> authorize(String roleId, String resourceIds) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = ResponseCode.SERVER_ERROR.getCode();

        if (StringHelper.isNotBlank(roleId)) {
            result = roleResourceService.authorize(roleId, resourceIds);
        } else {
            result = ResponseCode.PARAM_INVALID.getCode();
        }
        resultMap.put(IticConst.RESULT, result);
        return resultMap;
    }
}